package routes

import (
	"fmt"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm/clause"
	"jwgin/middleware"
	"jwgin/server/admin"
	"jwgin/server/ts"
	"jwgin/service"
)

func InitWebRoute() error {
	g := service.Conf.System
	gin.SetMode(g.Env)
	r := gin.New()
	pprof.Register(r)

	r.Use(middleware.Middlewares()...)

	rt := r.Group("")

	//test
	new(ts.BaseRouter).InitBaseRouter(rt)

	//前端

	//后台
	new(admin.BaseRouter).InitBaseRouter(rt)

	return r.Run(fmt.Sprintf(":%v", g.Addr))

}

//自动加表到api控制里
func casb(r *gin.Engine) {
	data := r.Routes()
	type s struct {
		UrlMode string `grom:"url_mode" json:"url_mode"`
		UrlPath string `grom:"url_path" json:"url_path"`
	}
	d := []s{}
	for _, v := range data {
		d1 := s{
			UrlMode: v.Method,
			UrlPath: v.Path,
		}
		d = append(d, d1)
	}

	service.G.Table("urls").Clauses(clause.OnConflict{UpdateAll: true}).Create(d)

}
